<?php

/*
 * This file is part of the symfony package.
 * (c) 2004-2006 Florian Klein < florian.klein@free.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * Propel Behavior to handle membership
 * @package    symfony
 * @subpackage plugin
 * @author     Florian Klein <florian.klein@free.fr>
 * @version    SVN: $Id: sfPropelOwnedBehavior.class.php 4634 2007-07-16 20:19:09Z florian $
 */

class sfPropelOwnedBehavior {
    
    /****************************************
     * 
     *  OWNED Behavior
     * 
     ****************************************/
    
    /**
    * Tests whether object is owned by passed object or object with given ID
    *
    * @param $object mixed BaseObject instance
    * @param $owner mixed sfUser instance
    * @return boolean 
    */
    public function isOwnedBy($object, $owner=null, $con=null) {
        $c = new Criteria;
        $c->add(SfOwnedPeer::OWNER_ID, $this->getOwnerId($owner));
        return SfOwnedPeer::doCount($this->getOwnersCriteria($object, $c)) >= 1;
    }
    
    private function getOwnerId($owner) {
        return ( $owner ? ( is_int($owner) ? $owner : $owner->getId() ) : sfContext::getInstance()->getUser()->getId());
    }
    
    /**
    * Gets an instance of 'owner' object
    *
    * @param $object mixed BaseObject instance
    * @return sfUser instance or (mixed)id
    * @todo Describe this in good english
    */
    public function getOwner($object) {
        return $this->getOwners($object, true);
    }
    
    /**
    * Sets an owner on a baseObject
    *
    * @param $object a baseObject instance
    * @param $owner a sfUser instance or id
    * @return this a baseObject
    * @todo Describe this in good english
    */
    public function setOwner($object, $owner) {
        return $this->setOwners($object, array($owner));
        return $this;
    }
    
    /**
    * Gets a collection of 'owner' object
    *
    * @param $object mixed BaseObject instance
    * @return Array on array of sfUser instance or (mixed)id collection
    * @todo Describe this in good english
    */
    public function getOwners($object, $one=false ) {
        $c = $this->getOwnersCriteria($object);
        return $one ? SfOwnedPeer::doSelectOne($c) : SfOwnedPeer::doSelect($c);
    }
    
    /**
    * Sets an owner on a baseObject
    *
    * @param $object a baseObject instance
    * @param $owner a sfUser instance or id
    * @return this a baseObject
    * @todo Describe this in good english
    */
    public function setOwners($object, array $owners) {
        foreach($owners as $owner) {
            $sf_owned = new SfOwned;
            $sf_owned->setOwnedId($object->getPrimaryKey());
            $sf_owned->setOwnerId($this->getOwnerId($owner));
            $sf_owned->save();
        }
        return $this;
    }
    
    /**
    * Gets a Criteria object that filters sfOwnedPeer
    *
    * @param $object mixed BaseObject instance
    * @return Criteria
    * @todo Describe this in good english
    */
    
    public function getOwnersCriteria($object, $criteria=null, $con=null ) {
        if(!$criteria) {
            $c = new Criteria;
        }
        else {
            $c = clone $criteria;
        }
        $c->add(SfOwnedPeer::PEER_NAME, get_class($object->getPeer()));
        $c->add(SfOwnedPeer::OWNED_ID, $object->getPrimaryKey());
        return $c;
    }
    
    
    
    /****************************************
     * 
     *  OWNER Behavior
     * 
     ****************************************/
    
    /**
    * Gets an owned objects collection
    *
    * @param $object sfUser instance
    * @return array of mixed BaseObjects instances
    * @todo Describe this in good english
    */
    public function getOwnedObjects($object) {
        $c = $this->getOwnersCriteria($object);
        return SfOwner::doSelect($c);
    }
    
    /**
    * Gets a Criteria object that filters sfOwnedPeer
    *
    * @param $object mixed BaseObject instance
    * @return Criteria
    * @todo Describe this in good english
    */
    public function getOwnedCriteria($object, $criteria=null, $con=null ) {
        if(!$criteria) {
            $c = new Criteria;
        }
        else {
            $c = clone $criteria;
        }
        $c->add(SfOwnedPeer::PEER_NAME, get_class($object->getPeer()));
        $c->add(SfOwnedPeer::OWNER_ID, $object->getId());
        return $c;
    }
}
